package com.whmskj.xjlhsc.view.service.impl;

import cn.dev33.satoken.fun.strategy.SaAutoRenewFunction;
import cn.hutool.core.collection.CollectionUtil;
import com.whmskj.xjlhbc.db.DataSourceType;
import com.whmskj.xjlhbc.db.DynamicDataSourceContextHolder;
import com.whmskj.xjlhsc.entity.Sb02EquipmentCounterAccount;
import com.whmskj.xjlhsc.entity.WbRecord;
import com.whmskj.xjlhsc.mapper.Sb02EquipmentCounterAccountMapper;
import com.whmskj.xjlhsc.view.entity.vo.DBJYiledVo;
import com.whmskj.xjlhsc.view.entity.vo.RecentlyEnergyVo;
import com.whmskj.xjlhsc.view.service.DeviceRealTimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.format.DateTimeFormatters;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 张
 * @version 1.0
 */

@Service
public class DeviceRealTimeServiceImpl implements DeviceRealTimeService {

    @Autowired
    private Sb02EquipmentCounterAccountMapper sb02EquipmentCounterAccountMapper;

    /**
     * 最近7天电耗曲线
     *
     * @param deviceId
     * @return
     */
    @Override
    public List<Map<String, Object>> recentlyEnergy(Integer deviceId) {
        Assert.notNull(deviceId, "机器号不能为空");
        List<Map<String, Object>> resultList=new ArrayList<>();
        LocalDate today = LocalDate.now();
        LocalDate lastDaySeven = today.minusDays(6);
        List<RecentlyEnergyVo> list = sb02EquipmentCounterAccountMapper.selectRecentlyEnergy(deviceId, today, lastDaySeven);
        Map<String, List<RecentlyEnergyVo>> collect = list.stream().collect(Collectors.groupingBy(RecentlyEnergyVo::getDay));
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        for (int i = 0; i < 7; i++) {
            LocalDate localDate = lastDaySeven.plusDays(i);
            String format = localDate.format(dateTimeFormatter);
            Map<String, Object> map = new HashMap<>();
            List<RecentlyEnergyVo> recentlyEnergyVos = collect.get(format);
            map.put("date", format);
            map.put("enerty", recentlyEnergyVos != null ? recentlyEnergyVos.get(0).getEnergy() : 0);
            resultList.add( map);
        }
        return resultList;
    }

    /**
     * 设备保养
     *
     * @param deviceId
     * @return
     */
    @Override
    public Map<String, Object> maintenance(Integer deviceId) {
        Assert.notNull(deviceId, "机器号不能为空");

        LocalDate now = LocalDate.now();
        String startTime = "";
        String endTime = "";
        int dayOfMonth = now.getDayOfMonth();

        if (dayOfMonth >= 20) {
            startTime = now.getYear() + "-" + now.getMonthValue() + "-" + 20;
        } else {
            startTime = now.getYear() + "-" + (now.getMonthValue() - 1) + "-" + 20;
        }
        endTime = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        Integer curMonthWbRecordCount = sb02EquipmentCounterAccountMapper.selectMaintenance(deviceId, startTime, endTime, 101);
        Integer curMonthNoWbRecordCount = sb02EquipmentCounterAccountMapper.selectMaintenance(deviceId, startTime, endTime, 102);
        startTime = now.getYear() + "-1-1";
        endTime = now.getYear() + "-12-30";
        Integer curYearWbRecordCount = sb02EquipmentCounterAccountMapper.selectMaintenance(deviceId, startTime, endTime, 101);
        Integer curYearWbNoRecordCount = sb02EquipmentCounterAccountMapper.selectMaintenance(deviceId, startTime, endTime, 102);
        return Map.of("curMonthWbRecordCount", curMonthWbRecordCount, "curMonthNoWbRecordCount", curMonthNoWbRecordCount,
                "curYearWbRecordCount", curYearWbRecordCount, "curYearWbNoRecordCount", curYearWbNoRecordCount);
    }

    @Override
    public Sb02EquipmentCounterAccount getDeviceInfo(Integer deviceId) {
        Assert.notNull(deviceId, "机器号不能为空");
        return sb02EquipmentCounterAccountMapper.selectSb02EquipmentCounterAccountById(deviceId);
    }

    @Override
    public Map<String, Object> workShopSuspension(Integer workShop) {
        Map<String, Object> resultMap = new HashMap<>();
        List<Map<String, Object>> list = sb02EquipmentCounterAccountMapper.selectDeviceSuspension(workShop);
        List<Map<String, Object>> temperature = getTemperature(workShop);
        resultMap.put("deviceSuspension", list);
        resultMap.put("temperature", temperature);
        return resultMap;
    }

    @Override
    public List<DBJYiledVo> getDBJYield(Integer mesId) {

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        LocalDate today = LocalDate.now();
        LocalDate lastDay = today.minusDays(6);

        String endTime = today.format(dateTimeFormatter);
        String startTime = lastDay.format(dateTimeFormatter);
        List<DBJYiledVo> dbjYiledVoList = sb02EquipmentCounterAccountMapper.selectDBJYield(mesId, startTime, endTime);
        Map<String, List<DBJYiledVo>> dbjListGroupDay = dbjYiledVoList.stream().collect(Collectors.groupingBy(DBJYiledVo::getDay));

        List<DBJYiledVo> resultList = new ArrayList<>();
        for (int i = 0; i < 7; i++) {
            String day = lastDay.plusDays(i).format(dateTimeFormatter);
            List<DBJYiledVo> dbjList = dbjListGroupDay.get(day);
            if (CollectionUtil.isNotEmpty(dbjList) && dbjList.size() >= 2) {
                if (dbjList.get(0).getShift().equals("早班")) {
                    resultList.add(dbjList.get(0));
                    resultList.add(dbjList.get(1));
                } else {
                    resultList.add(dbjList.get(1));
                    resultList.add(dbjList.get(0));
                }
            } else if (CollectionUtil.isEmpty(dbjList)) {
                DBJYiledVo zaoDbjYiledVo = new DBJYiledVo();
                zaoDbjYiledVo.setDay(day);
                zaoDbjYiledVo.setActualyieldkg(0.0);
                zaoDbjYiledVo.setCount(0);
                zaoDbjYiledVo.setShift("早班");
                DBJYiledVo wanDbjYiledVo = new DBJYiledVo();
                wanDbjYiledVo.setDay(day);
                wanDbjYiledVo.setActualyieldkg(0.0);
                wanDbjYiledVo.setCount(0);
                wanDbjYiledVo.setShift("晚班");
                resultList.add(zaoDbjYiledVo);
                resultList.add(wanDbjYiledVo);
            } else {
                if (dbjList.get(0).getShift().equals("早班")) {
                    resultList.add(dbjList.get(0));
                    DBJYiledVo wanDbjYiledVo = new DBJYiledVo();
                    wanDbjYiledVo.setDay(day);
                    wanDbjYiledVo.setActualyieldkg(0.0);
                    wanDbjYiledVo.setCount(0);
                    wanDbjYiledVo.setShift("晚班");
                    resultList.add(wanDbjYiledVo);
                } else {
                    DBJYiledVo zaoDbjYiledVo = new DBJYiledVo();
                    zaoDbjYiledVo.setDay(day);
                    zaoDbjYiledVo.setActualyieldkg(0.0);
                    zaoDbjYiledVo.setCount(0);
                    zaoDbjYiledVo.setShift("早班");
                    resultList.add(zaoDbjYiledVo);
                    resultList.add(dbjList.get(1));
                }
            }
        }
        return resultList;
    }

    @Override
    public List<LinkedHashMap<String, Object>> getYQJYield(Integer mesId) {
        List<LinkedHashMap<String, Object>> resultList = new ArrayList<>();
        LinkedHashMap<String, Object> linkedHashMap = sb02EquipmentCounterAccountMapper.selectYXJYield(mesId);
        if (CollectionUtil.isNotEmpty(linkedHashMap)) {
            Set<String> keySet = linkedHashMap.keySet();
            for (String key : keySet) {
                LinkedHashMap<String, Object> map = new LinkedHashMap<>();
                map.put("hour", key);
                map.put("value", linkedHashMap.get(key));
                resultList.add(map);
            }
        }else {
            for (int i = 0; i < 23; i++) {
                LinkedHashMap<String, Object> map = new LinkedHashMap<>();
                map.put("hour", i+"点");
                map.put("value", 0);
                resultList.add(map);
            }
        }
        return resultList;
    }

    private List<Map<String, Object>> getTemperature(Integer workshop) {
        DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.SC.name());
        List<Map<String, Object>> list = sb02EquipmentCounterAccountMapper.selectTemperature(workshop);
        DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.MASTER.name());
        return list;
    }

    public static void main(String[] args) {
        LocalDate today = LocalDate.now();
        LocalDate lastDaySeven = today.minusDays(6);
        System.out.println(today);
        System.out.println(lastDaySeven);
    }
}
